home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / aff.c < prev    next >
C/C++ Source or Header  |  1996-10-13  |  11KB  |  450 lines

  1. /*
  2.  *                              Aff.C
  3.  *
  4.  * Automatic File Forwarding utility.
  5.  */
  6.  
  7. /*
  8.  *                              History
  9.  *
  10.  * 90May15  HAW V1 Created.
  11.  */
  12.  
  13. /*
  14.  *                              Contents
  15.  *
  16.  *      crashout()              general crash routine
  17.  *      InitSystem()            open up appropriate data base files
  18.  *      main()                  main manager
  19.  *      ParseFileMap()          reads in CtdlAff.Sys
  20.  *      EatMapLine()            eats a line from a file map
  21.  *      RewriteFileMap()        writes out the updated file map
  22.  *      WriteFileList()         work fn that writes out a ctdlaff.sys entry
  23.  *      SystemWrite()           work fn writes out the indicated system
  24.  *      SendThemFiles()         setts up the .sfl files
  25.  *      UpdateSFLs()            updates the sfl file for the given system
  26.  *      WriteSFL()              This will write the .sfl file
  27.  *      CopyToTemp()            copies a name to the temporary list
  28.  *      ShowArguments()         usage
  29.  *      FileCmp()               helps find the string in a list
  30.  *      CheckSystems()          used to help search for a given system in list
  31.  */
  32.  
  33. #include "ctdl.h"
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <math.h>
  38. #include <ctype.h>
  39. #include <time.h>
  40. #include <dos.h>
  41. #include <proto/exec.h>
  42. #include <dos/dos.h>
  43. #include <pragmas/dos_pragmas.h>
  44. #include "exec/memory.h"
  45. #include "exec/ports.h"
  46. #include "exec/exec.h"
  47.  
  48. #ifndef NO_STAT
  49. #include "sys\stat.h"
  50. #endif
  51.  
  52. long getdateof(char *file);
  53.  
  54. extern FILE *upfd;
  55.  
  56. extern CONFIG    cfg;
  57. extern NetBuffer netBuf;
  58. extern int       thisNet;
  59. extern FILE      *netfl;
  60. extern NetTable  *netTab;
  61. extern char      *READ_ANY, *WRITE_ANY, *WRITE_TEXT;
  62.  
  63. void InitSystem(void);
  64. void ShowArguments(void);
  65. void ParseFileMap(void);
  66. void SendThemFiles(void);
  67. void RewriteFileMap(void);
  68.  
  69. /*
  70.  * crashout()
  71.  *
  72.  * This is general crash routine.  Needed by getNet, etc.
  73.  */
  74. void crashout(str)
  75. char *str;
  76. {
  77.     printf(str);
  78.     exit(1);
  79. }
  80.  
  81. /*
  82.  * InitSystem()
  83.  *
  84.  * This is used to open up appropriate data base files, and initialize buffers.
  85.  */
  86. void InitSystem()
  87. {
  88.     SYS_FILE name;
  89.  
  90.     /* Open database files. */
  91.     makeSysName(name, "ctdlnet.sys", &cfg.netArea);
  92.     openFile(name, &netfl);
  93.  
  94.     /* Now initialize data buffers. */
  95.     initNetBuf(&netBuf);
  96. }
  97.  
  98. /*
  99.  * main()
  100.  *
  101.  * The main manager for Aff.
  102.  */
  103. int main(int, char **);
  104. int main(argc, argv)
  105. int  argc;
  106. char **argv;
  107. {
  108.  
  109.     printf("AFF %s\nAutomatic File Forwarding\n%s\n", VERSION_NAME, COPYRIGHT);
  110.  
  111.     if (access(LOCKFILE, 0) != ERROR) {
  112.         printf("Please do not run AFF using Outside Commands.\n");
  113.         exit(1);
  114.     }
  115.  
  116.     if (argc > 1) {
  117.         ShowArguments();
  118.         exit(1);
  119.     }
  120.  
  121.     if (!readSysTab(FALSE, FALSE)) {
  122.         printf("Couldn't read system table.\n");
  123.         exit(1);
  124.     }
  125.  
  126.     InitSystem();       /* Open needed data files       */
  127.  
  128.     ParseFileMap();
  129.  
  130.     RewriteFileMap();   /* rewrite updated CtdlAff.Sys next     */
  131.  
  132.     SendThemFiles();    /* now send them files (if any)         */
  133.  
  134.     writeSysTab();
  135.  
  136.     return 0;
  137. }
  138.  
  139. #define FILE_NAME       0
  140. #define SYSTEM_NAME     1
  141. #define NO_FILE_NAME    2
  142.  
  143. char MapState = FILE_NAME;
  144. void *EatMapLine(char *line);
  145. void *CheckSystems();
  146. SListBase Files = { NULL, NULL, NULL, NULL, EatMapLine };
  147. SListBase Systems = { NULL, CheckSystems, NULL, NULL, NULL };
  148.  
  149. /* this structure is used to rebuild ctdlaff.sys */
  150. typedef struct {
  151.         char *Filename;
  152.         long Date;
  153.         SListBase SystemList;
  154. } FileList;
  155.  
  156. /* this structure is used in SystemList, above */
  157. typedef struct {
  158.         char *Systemname;
  159.         long Date;
  160. } SystemRec;
  161.  
  162. /* this structure is used for efficient updates of *.sfl */
  163. typedef struct {
  164.         char *Systemname;
  165.         SListBase NewFiles;     /* but this is just a list of strings */
  166. } SystemUpdate;
  167.  
  168. /*
  169.  * ParseFileMap()
  170.  *
  171.  * This function will read in CtdlAff.Sys.
  172.  */
  173. void ParseFileMap()
  174. {
  175.     SYS_FILE fn;
  176.  
  177.     makeSysName(fn, "ctdlaff.sys", &cfg.netArea);
  178.     if (!MakeList(&Files, fn, NULL))
  179.         crashout("Couldn't find CtdlAff.Sys.");
  180. }
  181.  
  182. /*
  183.  * EatMapLine()
  184.  *
  185.  * This function will eat a line from a file map.  Actions depend on
  186.  * MapState:
  187.  *
  188.  *      FILE_NAME: looking for a filename to parse.
  189.  *      SYSTEM_NAME: looking for a system to send file to.
  190.  *      NO_FILE_NAME: File for this doesn't exist, so ignore entries.
  191.  *
  192.  *      File is formatted:
  193.  *      <filename>
  194.  *      <system name>:<number>
  195.  *      <system name>:<number>
  196.  *      ...
  197.  *      <blank line>
  198.  *      <filename>
  199.  */
  200. void *EatMapLine(char *line)
  201. {
  202.     static FileList *data;      /* keep static so we know what's current*/
  203.     char *colon, work[200];
  204.     SystemRec *sysdata;
  205.     SystemUpdate *sysupdate, *System;
  206. #ifndef NO_STAT
  207.     struct stat buf;
  208. #endif
  209.  
  210.     switch (MapState) {
  211.     case FILE_NAME:
  212.         if (strLen(line) == 0) return NULL;     /* discard blank lines  */
  213.         if (access(line, 0) != 0) {             /* if file doesn't exist*/
  214.             MapState = NO_FILE_NAME;            /* then don't process   */
  215.             printf("Warning: file %s not found.\n", line);
  216.         }
  217.         else MapState = SYSTEM_NAME;    /* file exists */
  218.         data = GetDynamic(sizeof *data);
  219.         data->Filename = strdup(line);
  220.         if (MapState == SYSTEM_NAME) {
  221. #ifndef NO_STAT
  222.             stat(line, &buf);
  223.             data->Date = buf.st_mtime;
  224. #else
  225.             data->Date = getdateof(line);
  226. #endif
  227.         }
  228.         InitListValues(&data->SystemList, NULL, NULL, NULL, NULL);
  229.         return data;
  230.     case NO_FILE_NAME:
  231.         if (strLen(line) == 0) {        /* blank line, return to normal */
  232.             MapState = FILE_NAME;
  233.             return NULL;
  234.         }
  235.         if ((colon = strchr(line, ':')) != NULL) {
  236.             *colon++ = 0;
  237.             NormStr(colon);             /* kill leading spaces on date */
  238.             NormStr(line);              /* kill trailing spaces */
  239.             sysdata = GetDynamic(sizeof *sysdata);
  240.             sysdata->Systemname = strdup(line);
  241.             sysdata->Date = atol(colon);
  242.             AddData(&data->SystemList, sysdata, NULL, FALSE);
  243.         }
  244.         return NULL;
  245.     case SYSTEM_NAME:
  246.         if (strLen(line) == 0) {        /* blank line, return to normal */
  247.             MapState = FILE_NAME;
  248.         }
  249.         else {
  250.             if ((colon = strchr(line, ':')) != NULL) {
  251.                 *colon++ = 0;
  252.                 NormStr(colon);         /* kill leading spaces on date */
  253.                 NormStr(line);          /* kill trailing spaces */
  254.                 sysdata = GetDynamic(sizeof *sysdata);
  255.                 sysupdate = GetDynamic(sizeof *sysupdate);
  256.                 sysdata->Systemname = sysupdate->Systemname = strdup(line);
  257.                 sysdata->Date = atol(colon);
  258.                 AddData(&data->SystemList, sysdata, NULL, FALSE);
  259.                 if (data->Date > sysdata->Date) {
  260.                     if ((System = SearchList(&Systems, sysdata->Systemname))
  261.                                                                 == NULL) {
  262.                         System = sysupdate;
  263.                         InitListValues(&System->NewFiles,NULL,NULL,NULL,NULL);
  264.                         AddData(&Systems, System, NULL, FALSE);
  265.                     }
  266.                     else free(sysupdate);
  267.                     strcpy(work, data->Filename);
  268.                     AddData(&System->NewFiles, strdup(work), NULL, FALSE);
  269.                     if (searchNameNet(line, &netBuf) != ERROR)
  270.                         sysdata->Date = data->Date;     /* update record */
  271.                 }
  272.             }
  273.         }
  274.         return NULL;                    /* always return NULL here      */
  275.     default: crashout("Parsing failure!");
  276.     }
  277.     return NULL;
  278. }
  279.  
  280. static FILE *fd;
  281. /*
  282.  * RewriteFileMap()
  283.  *
  284.  * This function writes out the file map.
  285.  */
  286. void RewriteFileMap()
  287. {
  288.     SYS_FILE fn;
  289.     void WriteFileList();
  290.  
  291.     makeSysName(fn, "ctdlaff.sys", &cfg.netArea);
  292.     if ((fd = fopen(fn, WRITE_TEXT)) == NULL)
  293.         crashout("Couldn't update CtdlAff.Sys!");
  294.     RunList(&Files, WriteFileList);
  295.     fclose(fd);
  296. }
  297.  
  298. /*
  299.  * WriteFileList()
  300.  *
  301.  * This is a work fn that writes out a ctdlaff.sys entry.
  302.  */
  303. void WriteFileList(FileList *data)
  304. {
  305.         void SystemWrite();
  306.  
  307.         fprintf(fd, "%s\n", data->Filename);
  308.         RunList(&data->SystemList, SystemWrite);
  309.         fprintf(fd, "\n");      /* blank line separator */
  310. }
  311.  
  312. /*
  313.  * SystemWrite()
  314.  *
  315.  * This work fn writes out the indicated system to fd.
  316.  */
  317. void SystemWrite(SystemRec *data)
  318. {
  319.         fprintf(fd, "    %s : %ld\n", data->Systemname, data->Date);
  320. }
  321.  
  322. /*
  323.  * SendThemFiles()
  324.  *
  325.  * This function manages setting up the .sfl files.
  326.  */
  327. void SendThemFiles()
  328. {
  329.         void UpdateSFLs();
  330.  
  331.         RunList(&Systems, UpdateSFLs);
  332. }
  333.  
  334. void *FileCmp();
  335. SListBase TempList = { NULL, FileCmp, NULL, NoFree, NULL };
  336. /*
  337.  * UpdateSFLs()
  338.  *
  339.  * This updates the sfl file for the given system.  It works to ensure
  340.  * duplicates are not accidentally sent - that explains the complexity.
  341.  */
  342. void UpdateSFLs(SystemUpdate *data)
  343. {
  344.         void CopyToTemp(), WriteSFL();
  345.         int slot;
  346.         SYS_FILE fn;
  347.         char temp[10], realname[100];
  348.  
  349.         if ((slot = searchNameNet(data->Systemname, &netBuf)) == ERROR) {
  350.                 printf("Warning: no system '%s' known.\n", data->Systemname);
  351.                 return;
  352.         }
  353.         printf("System %s:", data->Systemname);
  354.         RunList(&data->NewFiles, CopyToTemp);
  355.         printf(".\n");
  356.  
  357.         sPrintf(temp, "%d.sfl", slot);
  358.         makeSysName(fn, temp, &cfg.netArea);
  359.  
  360.         if ((upfd = safeopen(fn, READ_ANY)) != NULL) {
  361.                 while (getSLNet(realname, upfd)) {
  362.                         /* NormalName(&buf, realname); */
  363.                         AddData(&TempList, strdup(realname), NULL, TRUE);
  364.                 }
  365.                 fclose(upfd);
  366.                 unlink(fn);
  367.         }
  368.  
  369.         if ((upfd = safeopen(fn, WRITE_ANY)) != NULL) {
  370.                 RunList(&TempList, WriteSFL);
  371.                 fclose(upfd);
  372.         }
  373.  
  374.         netBuf.nbflags.send_files = TRUE;
  375.         putNet(slot, &netBuf);
  376.         KillList(&TempList);
  377. }
  378.  
  379. /*
  380.  * WriteSFL()
  381.  *
  382.  * This will update the .sfl file.
  383.  */
  384. void WriteSFL(char *name)
  385. {
  386.         struct fl_send buf;
  387.         void SetBuffer(char *name, struct fl_send *buf);
  388.  
  389.         SetBuffer(name, &buf);
  390.         putSLNet(buf, upfd);
  391. }
  392.  
  393. /*
  394.  * CopyToTemp()
  395.  *
  396.  * This function will copy a name to the temporary list with no dups.
  397.  */
  398. void CopyToTemp(char *name)
  399. {
  400.         printf(" %s", name);
  401.         AddData(&TempList, name, NULL, TRUE);
  402. }
  403.  
  404. /*
  405.  * ShowArguments()
  406.  *
  407.  * This will show off arguments.
  408.  */
  409. void ShowArguments()
  410. {
  411.         printf("usage: Aff\n");
  412. }
  413.  
  414. /*
  415.  * FileCmp()
  416.  *
  417.  * This function helps find the designated string in a list of strings.
  418.  */
  419. void *FileCmp(char *n1, char *n2)
  420. {
  421.         if (strCmpU(n1, n2) == SAMESTRING) return n1;
  422.         return NULL;
  423. }
  424.  
  425. /*
  426.  * CheckSystems()
  427.  *
  428.  * This function is used to help search for a given system in a list.
  429.  */
  430. void *CheckSystems(SystemRec *s, char *t)
  431. {
  432.         if (stricmp(s->Systemname, t) == SAMESTRING) return s;
  433.         return NULL;
  434. }
  435.  
  436. long getdateof(char *file)
  437.   {
  438.   long thetime;
  439.   struct FileInfoBlock *info;
  440.   info = (struct FileInfoBlock *)calloc(1, sizeof(struct FileInfoBlock));
  441.   if( dfind(info,file,0) == 0 )
  442.     {
  443.     thetime = info->fib_Date.ds_Days*24*3600;
  444.     thetime+= info->fib_Date.ds_Minute;
  445.     }
  446.   else  thetime = 0;
  447.   free(info);
  448.   return thetime;
  449.   }
  450.